package com.cs342.uwosh.cloudycontacts;

import com.cs342.uwosh.cloudycontacts.tasks.db.AddContactTask;
import com.cs342.uwosh.cloudycontacts.tasks.db.GetAllContactTask;
import com.cs342.uwosh.cloudycontacts.MainActivity;
import com.cs342.uwosh.cloudycontacts.tasks.db.UpdateContactTask;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddContact extends Activity {

	String id = "";
	Contact currentContact;
	int state = 0;
	public static final int[] DEFAULT_THEME_ARRAY = 
			new int[] 
					{
					R.color.background_color_theme_default,
					R.color.category_btn_color_theme_default,
					R.color.list_item_color_theme_default,
					R.layout.custom_checkbox_theme_default,
					R.color.button_color_theme_default,
					R.style.ButtonText_theme_default,
					R.style.ButtonText_theme_default,
					R.style.ListItemText_theme_default,
					R.style.EditText_theme_default,
					R.color.category_btn_color_theme_default_pressed
					};
	
	public static final int[] ONE_THEME_ARRAY =
			new int[] 
					{
					R.color.background_color_theme_one,
					R.color.category_btn_color_theme_one,
					R.color.list_item_color_theme_one,
					R.layout.custom_checkbox_theme1_default,
					R.color.button_color_theme_one,
					R.style.ButtonText_theme1_default,
					R.style.ButtonText_theme1_default,
					R.style.ListItemText_theme1_default,
					R.style.EditText_theme1_default,
					R.color.category_btn_color_theme_one_pressed
					};
	
	public static final int[] TWO_THEME_ARRAY =
			new int[] 
					{
					R.color.background_color_theme_two,
					R.color.category_btn_color_theme_two,
					R.color.list_item_color_theme_two,
					R.layout.custom_checkbox_theme2_default,
					R.color.button_color_theme_two,
					R.style.ButtonText_theme2_default,
					R.style.ButtonText_theme2_default,
					R.style.ListItemText_theme2_default,
					R.style.EditText_theme2_default,
					R.color.category_btn_color_theme_two_pressed
					};
	
	public static final int[] THREE_THEME_ARRAY =
			new int[] 
					{
					R.color.background_color_theme_three,
					R.color.category_btn_color_theme_three,
					R.color.list_item_color_theme_three,
					R.layout.custom_checkbox_theme3_default,
					R.color.button_color_theme_three,
					R.style.ButtonText_theme3_default,
					R.style.ButtonText_theme3_default,
					R.style.ListItemText_theme3_default,
					R.style.EditText_theme3_default,
					R.color.category_btn_color_theme_three_pressed
					};
	
	
	public static String THEME_DEFAULT = "DEFAULT";

	public static String THEME_ONE = "THEME_ONE";

	public static String THEME_TWO = "THEME_TWO";

	public static String THEME_THREE = "THEME_THREE";

	public static String CURRENT_THEME = THEME_DEFAULT;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_contact);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			Button addB = (Button) findViewById(R.id.addButton);
			addB.setText("Update!");

			Intent intent = getIntent();
			id = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

			currentContact = MainActivity.allContacts.get(id);

			EditText na = (EditText) findViewById(R.id.nameText);
			na.setText(currentContact.getName());

			EditText ad = (EditText) findViewById(R.id.addrText);
			ad.setText(currentContact.getAddress());

			EditText ce = (EditText) findViewById(R.id.cellText);
			ce.setText(currentContact.getCellNumber());

			EditText alt = (EditText) findViewById(R.id.altText);
			alt.setText(currentContact.getAltNumber());

			EditText em = (EditText) findViewById(R.id.emailText);
			em.setText(currentContact.getEmail());

			EditText com = (EditText) findViewById(R.id.comText);
			com.setText(currentContact.getComments());

			state = 1;
		} else {
			state = 0;
		}
	}

	public void onResume() {
		super.onResume();
		if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_DEFAULT)) {
			applyTheme(MainActivity.DEFAULT_THEME_ARRAY);
		} else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_ONE)) {
			applyTheme(MainActivity.ONE_THEME_ARRAY);
		} else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_TWO)) {
			applyTheme(MainActivity.TWO_THEME_ARRAY);
		} else if (MainActivity.CURRENT_THEME.equals(MainActivity.THEME_THREE)) {
			applyTheme(MainActivity.THREE_THEME_ARRAY);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_add_contact, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.themes:
			generateThemesDialog();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	private   void generateThemesDialog()
	{
		final CharSequence[] themesList = new CharSequence[] 
				{
				THEME_DEFAULT,
				THEME_ONE,
				THEME_TWO,
				THEME_THREE
				};
		AlertDialog.Builder themesDialog = new AlertDialog.Builder(this);
		
		themesDialog.setTitle("Choose Your Theme");
		
		themesDialog.setSingleChoiceItems(themesList, -1, new DialogInterface.OnClickListener()
		{
			
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				if (themesList[which].equals(THEME_DEFAULT))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_DEFAULT;
					applyTheme(DEFAULT_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				else if (themesList[which].equals(THEME_ONE))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_ONE;
					applyTheme(ONE_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				else if (themesList[which].equals(THEME_TWO))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_TWO;
					applyTheme(TWO_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				else if (themesList[which].equals(THEME_THREE))
				{
					MainActivity.CURRENT_THEME = MainActivity.THEME_THREE;
					applyTheme(THREE_THEME_ARRAY);
					new GetAllContactTask().execute();
					dialog.cancel();
				}
				
			}
		});
		themesDialog.show();
	}
	/*
	 * Breakdown of indexing of the themes arrays [0] = App background color [1]
	 * = TextView category buttons at the top of MainActivity [2] = List items
	 * for main list of contacts [3] = CheckBoxPreferences on the Create/Edit
	 * categories activity [4] = All of the apps actual buttons [5] = Category
	 * "buttons" text [6] = All actual buttons text [7] = ListView text [8] =
	 * EditText text
	 */
	private void applyTheme(int[] themeToApply) {
		findViewById(R.id.addContactMainLayout).setBackgroundResource(
				themeToApply[0]);
		applyThemeToTextView(new TextView[] {
				(TextView) findViewById(R.id.nameText),
				(TextView) findViewById(R.id.nameTag),
				(TextView) findViewById(R.id.addrText),
				(TextView) findViewById(R.id.addrTag),
				(TextView) findViewById(R.id.cellText),
				(TextView) findViewById(R.id.cellTag),
				(TextView) findViewById(R.id.altText),
				(TextView) findViewById(R.id.altTag),
				(TextView) findViewById(R.id.emailText),
				(TextView) findViewById(R.id.emailTag),
				(TextView) findViewById(R.id.comText),
				(TextView) findViewById(R.id.comTag) }, themeToApply[8]);
		findViewById(R.id.addButton).setBackgroundResource(themeToApply[4]);
		Button b = (Button) findViewById(R.id.addButton);
		b.setTextAppearance(this, themeToApply[6]);
	}

	private void applyThemeToTextView(TextView[] textViewArray, int textTheme) {
		for (TextView textview : textViewArray) {
			textview.setTextAppearance(this, textTheme);
		}
	}

	public void createContact(View view) {
		EditText n = (EditText) findViewById(R.id.nameText);
		EditText a = (EditText) findViewById(R.id.addrText);
		EditText c = (EditText) findViewById(R.id.cellText);
		EditText al = (EditText) findViewById(R.id.altText);
		EditText e = (EditText) findViewById(R.id.emailText);
		EditText co = (EditText) findViewById(R.id.comText);
		String name = n.getText().toString();
		String address = a.getText().toString();
		String cell = c.getText().toString();
		String alternate = al.getText().toString();
		String email = e.getText().toString();
		String comment = co.getText().toString();
		int category = MainActivity.categoryViewed;
		if (state == 0) {
			Contact adder = new Contact(name, address, cell, alternate, email,
					comment, category);
			new AddContactTask().execute(adder);
		} else if (state == 1) {
			currentContact.setName(name);
			currentContact.setAddress(address);
			currentContact.setCellNumber(cell);
			currentContact.setAltNumber(alternate);
			currentContact.setEmail(email);
			currentContact.setComments(comment);
			new UpdateContactTask().execute(currentContact);
		}
		// new GetAllContactTask().execute();
		finish();
	}

}